home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte16 / getnpi.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  8KB  |  259 lines

  1.  
  2. #include <windows.h>  
  3. #include "GetNPI.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define NUMBERCOLORS 16
  108.  
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static LPLOGPALETTE pLogPal;
  113. static HPALETTE     hPal;
  114.  
  115.    switch( uMsg )
  116.    {
  117.        case WM_CREATE : 
  118.               {
  119.                  int i;
  120.  
  121.                  // Allocate space for palette entries.
  122.                  //....................................
  123.                  pLogPal = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
  124.                                       sizeof( LOGPALETTE ) +
  125.                                       (NUMBERCOLORS*sizeof( PALETTEENTRY )) );
  126.  
  127.                  // Initialize palette entries with 15 shades of gray.
  128.                  //...................................................
  129.                  pLogPal->palVersion    = 0x300;
  130.                  pLogPal->palNumEntries = NUMBERCOLORS;
  131.  
  132.                  for ( i = 0; i < NUMBERCOLORS-1; i++ )
  133.                  {
  134.                     pLogPal->palPalEntry[i].peRed   = 255 - (i*15);
  135.                     pLogPal->palPalEntry[i].peGreen = 255 - (i*15);
  136.                     pLogPal->palPalEntry[i].peBlue  = 255 - (i*15);
  137.                     pLogPal->palPalEntry[i].peFlags = PC_RESERVED;
  138.                  }
  139.  
  140.                  // Set black palette entry.
  141.                  //.........................
  142.                  pLogPal->palPalEntry[i].peRed   = 0;
  143.                  pLogPal->palPalEntry[i].peGreen = 0;
  144.                  pLogPal->palPalEntry[i].peBlue  = 0;
  145.                  pLogPal->palPalEntry[i].peFlags = PC_RESERVED;
  146.  
  147.                  // Create palette.
  148.                  //................
  149.                  hPal = CreatePalette( pLogPal );
  150.               }
  151.               break;
  152.  
  153.        case WM_PAINT :
  154.               {
  155.                  PAINTSTRUCT ps;
  156.                  HBRUSH      hBrush;
  157.                  int         i;
  158.  
  159.                  BeginPaint( hWnd, &ps );
  160.  
  161.                  // Select and realize palette.
  162.                  //............................
  163.                  SelectPalette( ps.hdc, hPal, FALSE );
  164.                  RealizePalette( ps.hdc );
  165.  
  166.                  SelectObject( ps.hdc, GetStockObject( NULL_PEN ) );
  167.  
  168.                  // Paint a sample of each color in the palette.
  169.                  //.............................................
  170.                  for ( i = 0; i < NUMBERCOLORS; i++ )
  171.                  {
  172.                     hBrush = CreateSolidBrush( PALETTEINDEX( i ) );
  173.                     SelectObject( ps.hdc, hBrush );
  174.                     Rectangle( ps.hdc, i*10, 0, (i*10)+11, 50 );
  175.                     SelectObject( ps.hdc, GetStockObject( WHITE_BRUSH ) );
  176.                     DeleteObject( hBrush );
  177.                  }
  178.  
  179.                  EndPaint( hWnd, &ps );
  180.               }
  181.               break;
  182.  
  183.       case WM_COMMAND :
  184.               switch( LOWORD( wParam ) )
  185.               {
  186.                  case IDM_TEST :
  187.                        {
  188.                           COLORREF color;
  189.                           HDC      hDC = GetDC( hWnd );
  190.  
  191.                           color = GetNearestPaletteIndex( hPal, 
  192.                                                           RGB( 255, 0, 0 ) );
  193.  
  194.                           // Display a rectangle with the color found.
  195.                           //..........................................
  196.                           if ( color != CLR_INVALID )
  197.                           {
  198.                              HBRUSH hBrush;
  199.  
  200.                              hBrush = CreateSolidBrush( PALETTEINDEX(color) );
  201.  
  202.                              SelectObject( hDC, hBrush );
  203.                              Rectangle( hDC, 10, 60, 50, 100 );
  204.                              SelectObject( hDC, GetStockObject(WHITE_BRUSH) );
  205.  
  206.                              DeleteObject( hBrush );
  207.                           }
  208.  
  209.                           ReleaseDC( hWnd, hDC );
  210.                        }
  211.                        break;
  212.  
  213.                  case IDM_ABOUT :
  214.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  215.                         break;
  216.  
  217.                  case IDM_EXIT :
  218.                         DestroyWindow( hWnd );
  219.                         break;
  220.               }
  221.               break;
  222.       
  223.       case WM_DESTROY :
  224.               HeapFree( GetProcessHeap(), 0, pLogPal );
  225.               DeleteObject( hPal );
  226.               PostQuitMessage(0);
  227.               break;
  228.  
  229.       default :
  230.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  231.    }
  232.  
  233.    return( 0L );
  234. }
  235.  
  236.  
  237. LRESULT CALLBACK About( HWND hDlg,           
  238.                         UINT message,        
  239.                         WPARAM wParam,       
  240.                         LPARAM lParam)
  241. {
  242.    switch (message) 
  243.    {
  244.        case WM_INITDIALOG: 
  245.                return (TRUE);
  246.  
  247.        case WM_COMMAND:                              
  248.                if (   LOWORD(wParam) == IDOK         
  249.                    || LOWORD(wParam) == IDCANCEL)    
  250.                {
  251.                        EndDialog(hDlg, TRUE);        
  252.                        return (TRUE);
  253.                }
  254.                break;
  255.    }
  256.  
  257.    return (FALSE); 
  258. }
  259.